package com.yzt.message.widget.chatinput.widget;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yzt.message.R;
import com.yzt.message.widget.chatinput.utils.EmojiUtil;

import java.util.List;


/**
 * author : Irene
 * e-mail : 821970751@qq.com
 * date   : 2020/5/19 16:43
 * desc   :
 */
public class EmojiItemFragment extends Fragment {

    private List<AndroidEmoji1.EmojiInfo> list;
    private int column;
    private int type;

    private RecyclerView mEmojiRv;
    private EmojiItemAdapter mEmojiItemAdapter;
    private CustomEmojiView.OnSelectEmojiListener listener;

    public static EmojiItemFragment newInstance(List<AndroidEmoji1.EmojiInfo> list, int column, int type, CustomEmojiView.OnSelectEmojiListener listener) {
        EmojiItemFragment fragment = new EmojiItemFragment();
        fragment.list = list;
        fragment.column = column;
        fragment.type = type;
        fragment.listener = listener;
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_emojii_tem, null);
        initView(rootView);
        return rootView;
    }

    public void initView(View view) {
        list = AndroidEmoji1.getEmojiList();
        mEmojiRv = view.findViewById(R.id.emoji_rv);
        //for Span count should be at least 1. Provided 0
        if (column <= 0)
            column = 7;
        mEmojiRv.setLayoutManager(new GridLayoutManager(getActivity(), column));
        if (list != null) {
            mEmojiItemAdapter = new EmojiItemAdapter(getActivity(), column);
            mEmojiRv.setAdapter(mEmojiItemAdapter);
            mEmojiItemAdapter.initData(list);
            mEmojiItemAdapter.setOnClickEmojiSelectListener(new EmojiItemAdapter.OnClickEmojiSelectListener() {
                @Override
                public void selectIcon(AndroidEmoji1.EmojiInfo emojiBean) {
                    final String content = getString(emojiBean.strId);
                    final String emojiPath = emojiBean.resId + "";
                    if (listener != null) {
                        System.out.println("IreneBond emojiPath: " + emojiPath + " gameResult: " + EmojiUtil.getGameResult(content) + " content: " + content);
                        //排序作弊
                        listener.selectEmoji(content, emojiPath, EmojiUtil.getGameResult(content), type);
                    }
                }
            });
        }
    }
}
